package XPath;

import java.io.IOException;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import org.jdom2.xpath.jaxen.JaxenXPathFactory;

public class JDomXPath {

	public static void main(String[] args) {
		
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build("sample.xml");
			
			Element root = document.getRootElement();
			
			XPathFactory factory = JaxenXPathFactory.instance();
			XPathExpression<Object> xpe = factory.compile("//student[./gender='male']");
			
			List<Object> objLists = xpe.diagnose(root, false).getResult();
			
			for(Object obj : objLists) {
				Element element = (Element) obj;
				System.out.println(element);
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
